import React from "react";
import { Button, Checkbox, Form, Input } from "antd";
import axios from "axios";
import { useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
import { username } from "../start/index";
function Index() {
  const dispatch = useDispatch();
  const naviagte = useNavigate();
  const onFinish = (values) => {
    console.log("Success:", values);
    axios.post("/api/login", values).then((res) => {
      localStorage.setItem("token", res.data.token);

      dispatch(username(res.data.token));
      naviagte("/home");
      alert("登陆成功");
      localStorage.setItem("jykl", res.data.ming);
      localStorage.setItem(
        "https://img-s.msn.cn/tenant/amp/entityid/AA1GMso8.img?w=768&h=1024&m=6",
        res.data.image
      );
    });
  };
  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <div>
      <div
        style={{
          width: "300px",
          height: "300px",
          background: "#cccccc",
          marginTop: "45vh",
          marginLeft: "40vw",
        }}
      >
        <Form
          name="basic"
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 16 }}
          style={{ maxWidth: 600 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="账号"
            name="account"
            rules={[{ required: true, message: "请输入账号" }]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="密码"
            name="password"
            rules={[{ required: true, message: "请输入密码" }]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item label={null}>
            <Button type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

export default Index;
